package com.mikhailenko.transportMVC.service.es.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mikhailenko.transportMVC.exeption.es.InputFakeNotfoundException;
import com.mikhailenko.transportMVC.model.es.InputEntityFake;
import com.mikhailenko.transportMVC.repository.es.InputFakeRepository;
import com.mikhailenko.transportMVC.service.es.InputFakeService;

@Service
public class InputFakeServiceImpl implements InputFakeService {

	@Autowired
	private InputFakeRepository inputRepository;
	
	@Override
	@Transactional
	public InputEntityFake create(InputEntityFake input) {
		InputEntityFake createdInput = input;
		return inputRepository.saveAndFlush(createdInput);
	}

	@Override
	@Transactional(rollbackFor = InputFakeNotfoundException.class)
	public InputEntityFake delete(int id) throws InputFakeNotfoundException {
		InputEntityFake deletedInput = inputRepository.findOne(id);
		
		if (deletedInput == null){
			throw new InputFakeNotfoundException();
		}
		
		inputRepository.delete(deletedInput);
		return deletedInput;
	}

	@Override
	@Transactional
	public List<InputEntityFake> findAll() {
		return inputRepository.findAll();
	}

	@Override
	@Transactional(rollbackFor = InputFakeNotfoundException.class)
	public InputEntityFake update(InputEntityFake input)
			throws InputFakeNotfoundException {
		InputEntityFake updatedInput = inputRepository.findOne(input.getId());
		
		if (updatedInput == null){
			throw new InputFakeNotfoundException();
		}
		
		updatedInput.setName(input.getName());
		updatedInput.setType(input.isType());
		updatedInput.setUsed(input.isUsed());
		return updatedInput;
	}

	@Override
	@Transactional
	public InputEntityFake findById(int id) {
		return inputRepository.findOne(id);
	}

	@Override
	public List<InputEntityFake> findByType(boolean type) {
		return inputRepository.findByType(type);
	}

	@Override
	public InputEntityFake findByName(String name) {
		return inputRepository.findByName(name);
	}

}
